<template>
  <div id="app">
    <div class="background">
      <img :src="imgSrc" width="100%" height="100%" alt="" />
    </div>
    <div class="front" >
    <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <h3 class="login_title">忘记密码</h3>
      <el-form-item label="邮箱" prop="email" >
        <el-input type="email" v-model="ruleForm.email" autocomplete="off" placeholder="请输入邮箱" maxlength="50"></el-input>
      </el-form-item>
      <el-form-item label="新密码" prop="newPwd" >
        <el-input type="password" v-model="ruleForm.newPwd" autocomplete="off" placeholder="请输入新密码6位以上"  maxlength="50"></el-input>
      </el-form-item>
      <el-form-item label="邮箱验证码" prop="verification" >
        <el-input  v-model="ruleForm.verification" autocomplete="off" placeholder="请输入邮箱验证码" maxlength="4"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="subCode" v-bind:disabled="IfDisabled">
          <p v-show="sendCode" >获取验证码</p>
          <P v-show="!sendCode">{{authTime}} S后重新发送</P>
        </el-button>
        <el-button type="primary" @click="SubnewPwd">确认</el-button>
      </el-form-item>
    </el-form>
      </div>
  </div>
</template>

<script>
import axios from 'axios'

export default {
  name: 'ForgetPassword',
  data () {
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入新密码'))
      } else {
        callback()
      }
    }
    return {
      imgSrc: require('../assets/Login.jpg'),
      sendCode: true, // 发送验证码按钮显示
      authTime: 60, // 倒计时
      IfDisabled: false,
      ruleForm: {
        email: '',
        newPwd: '',
        verification: ''
      },
      rules: {
        newPwd: [
          { validator: validatePass, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    subCode: function () {
      // alert(this.ruleForm.email)
      if (this.ruleForm.email === '') {
        alert('请输入邮箱')
      } else {
        axios.post(`user/personal/code`, {
          email: this.ruleForm.email
        }).then(res => {
          console.log(res)
        }).catch(err => {
          alert(err)
        })
        this.IfDisabled = true
        this.timer()
      }
    },
    timer: function () {
      this.sendCode = false
      if (this.authTime > 0) {
        this.authTime--
        this.btntxt = this.authTime + 's后重新获取'
        setTimeout(this.timer, 1000)
      } else {
        this.authTime = 60
        this.btntxt = '获取验证码'
        this.sendCode = true
        this.IfDisabled = false
      }
    },
    SubnewPwd: function () {
      // alert(this.ruleForm.newPwd)
      if (this.ruleForm.newPwd === '') {
        alert('请设置新密码')
      } else {
        axios.post(`user/personal/reset`, {
          email: this.ruleForm.email,
          code: this.ruleForm.verification,
          newPwd: this.ruleForm.newPwd
        }).then(res => {
          if (res.data.status === 100) {
            alert(res.data.data.msg)
            return this.$router.push({
              path: '/login'
            })
          } else {
            alert('提交失败')
          }
        }).catch(err => {
          alert(err)
        })
      }
    }
  }
}
</script>

<style scoped>
.demo-ruleForm {
  border-radius: 15px;
  background-clip: padding-box;
  margin: 90px auto;
  width: 400px;
  padding: 35px 35px 15px 35px;
  background: #fff;
  border: 1px solid #eaeaea;
  box-shadow: 0 0 25px #cac6c6;
}

.login_title {
  margin: 0px auto 40px auto;
  text-align: center;
  color: #505458;
}

.background{
  width:100%;
  height:100%;  /**宽高100%是为了图片铺满屏幕 */
  z-index:-1;
  position: absolute;
}

.front{
  z-index:1;
  /*position: absolute;*/
  margin-top: 0px ;
  text-align: center;
}
</style>
